#include "LV_GUI_APP.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "lvgl.h"
#include "lv_port_disp.h"
#include "esp_log.h"


static const char* TAG = "lv_gui_app";

LV_IMG_DECLARE(hutao0);
LV_IMG_DECLARE(hutao1);
LV_IMG_DECLARE(hutao2);
LV_IMG_DECLARE(hutao3);
LV_IMG_DECLARE(hutao4);
LV_IMG_DECLARE(hutao5);
LV_IMG_DECLARE(hutao6);
LV_IMG_DECLARE(hutao7);
LV_IMG_DECLARE(hutao8);
LV_IMG_DECLARE(hutao9);
LV_IMG_DECLARE(hutao10);


LV_IMG_DECLARE(astronaut1);
LV_IMG_DECLARE(astronaut2);
LV_IMG_DECLARE(astronaut3);
LV_IMG_DECLARE(astronaut4);
LV_IMG_DECLARE(astronaut5);
LV_IMG_DECLARE(astronaut6);
LV_IMG_DECLARE(astronaut7);
LV_IMG_DECLARE(astronaut8);
LV_IMG_DECLARE(astronaut9);
LV_IMG_DECLARE(astronaut10);
LV_IMG_DECLARE(astronaut11);
LV_IMG_DECLARE(astronaut12);
LV_IMG_DECLARE(astronaut13);
LV_IMG_DECLARE(astronaut14);

static esp_timer_handle_t lvgl_timer_handle = NULL;


static IRAM_ATTR void lv_timer_cb(void *arg)
{
   lv_tick_inc(1);
}

static esp_timer_create_args_t lvgl_timer = {
    .callback = &lv_timer_cb,
    .arg = NULL,
    .name ="lvgl_timer",
    .dispatch_method = ESP_TIMER_TASK
};

esp_err_t _lv_timer_create(void)
{
   esp_err_t err = esp_timer_create(&lvgl_timer, &lvgl_timer_handle);
   err = esp_timer_start_periodic(lvgl_timer_handle, 1000); // 1毫秒回调
   if (err != ESP_OK)
   {
      ESP_LOGE(TAG, "lvgl定时器创建成功\r\n");
   }
   return err;
}

void lv_img_timer_test_cb(lv_timer_t timer)
{
   static lv_obj_t * img_temp = NULL;
   static uint8_t  i = 0;
   if(img_temp==NULL)
   {
      img_temp = lv_img_create(lv_scr_act());
      lv_obj_align(img_temp,LV_ALIGN_CENTER,0,0);
   }
   switch (i)
   {
   case 0:lv_img_set_src(img_temp,&hutao0);break;
   case 1:lv_img_set_src(img_temp,&hutao1);break;
   case 2:lv_img_set_src(img_temp,&hutao2);break;
   case 3:lv_img_set_src(img_temp,&hutao3);break;
   case 4:lv_img_set_src(img_temp,&hutao4);break;
   case 5:lv_img_set_src(img_temp,&hutao5);break;
   case 6:lv_img_set_src(img_temp,&hutao6);break;
   case 7:lv_img_set_src(img_temp,&hutao7);break;
   case 8:lv_img_set_src(img_temp,&hutao8);break;
   case 9:lv_img_set_src(img_temp,&hutao9);break;
   case 10:lv_img_set_src(img_temp,&hutao10);break;
   default:
      break;
   }
   i++;
   if(i==11)
   {
      i = 0;
   }
}

const lv_img_dsc_t *hutao_img_list[11] = {
    &hutao0,
    &hutao1,
    &hutao2,
    &hutao3,
    &hutao4,
    &hutao5,
    &hutao6,
    &hutao7,
    &hutao8,
    &hutao9,
    &hutao10,
};

const lv_img_dsc_t *astronaut_img_list[14] = {
    &astronaut1,
    &astronaut2,
    &astronaut3,
    &astronaut4,
    &astronaut5,
    &astronaut6,
    &astronaut7,
    &astronaut8,
    &astronaut9,
    &astronaut10,
    &astronaut11,
    &astronaut12,
    &astronaut13,
    &astronaut14,
};

IRAM_ATTR void lvgl_task(void *arg)
{
   //lv_timer_create(lv_img_timer_test_cb,40,NULL);
   lv_obj_t *animi_img_hutao = lv_animimg_create(lv_scr_act());  //创建一个obj对象，用来显示
   lv_animimg_set_duration(animi_img_hutao, 650);
   lv_animimg_set_src(animi_img_hutao, (lv_img_dsc_t**) astronaut_img_list, 14);
   lv_animimg_set_repeat_count(animi_img_hutao, LV_ANIM_REPEAT_INFINITE);
   lv_animimg_start(animi_img_hutao);
   lv_obj_center(animi_img_hutao);

   while (true)
   {
      vTaskDelay(((1)/ portTICK_PERIOD_MS));
      lv_task_handler();
   }
}

void GUI_APP_init(void)
{
   lv_init();           // lvgl内核初始化
   lv_port_disp_init(); // lvgl显示接口初始化
   _lv_timer_create();
   xTaskCreatePinnedToCore(lvgl_task, "lvgl_task", 4096, NULL, 2, NULL, APP_CPU_NUM);
}
